package com.itany.service.impl;

import com.itany.entity.Notice;
import com.itany.entity.Permission;
import com.itany.entity.PermissionExample;
import com.itany.mapper.NoticeMapper;
import com.itany.mapper.PermissionMapper;
import com.itany.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper mapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Map<String, Object>> queryByParams(String name, String modelName) {
        List<Permission> list = mapper.selectByParams(name,modelName);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if(!list.isEmpty()){
            for (Permission permission : list) {
                Map<String, Object> u = new HashMap<>();
                u.put("id",permission.getId());
                u.put("name",permission.getName());
                u.put("url",permission.getUrl());
                u.put("parentName",permission.getParentName());
                u.put("parentId",permission.getParentid());
                mapList.add(u);
            }
            return mapList;
        }else{
            return null;
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Permission queryById(Integer id) {
        Permission permission = mapper.selectByPrimaryKey(id);
        List<Permission> list = mapper.selectTop(id);
        permission.setModules(list);
        return permission;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public void modifyContent(Integer id,String name, Integer moduleId) {
        mapper.updateById(id,name,moduleId);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Map<String, Object>> queryByRole() {
        List<Permission> list = mapper.selectNull();
        List<Map<String, Object>> mapList = new ArrayList<>();
        if(!list.isEmpty()){
            for(Permission permission : list){
                PermissionExample example = new PermissionExample();
                example.or().andParentidEqualTo(permission.getId());
                List<Permission> list1 = mapper.selectByExample(example);
                Map<String, Object> u = new HashMap<>();
                u.put("id",permission.getId());
                u.put("text",permission.getName());
                u.put("state","closed");
                List<Map<String, Object>> mapList1 = new ArrayList<>();
                if(!list1.isEmpty()){
                    for(Permission permission1 : list1){
                        Map<String, Object> map = new HashMap<>();
                        map.put("id",permission1.getId());
                        map.put("text",permission1.getName());
                        mapList1.add(map);
                    }
                    u.put("children",mapList1);
                }else{
                    u.put("children",null);
                }
                mapList.add(u);
            }
            return mapList;
        }else{
            return null;
        }
    }
}
